package com.chenyue.cm.base.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenyue.cm.base.bean.PageVo;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.base.bean.Pageable;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author Origin
 */
public abstract class BaseService<T, ID extends Serializable> extends ServiceImpl<BaseMapper<T>, T> implements BaseServiceIface<T> {

    public BaseMapper<T> baseDao;

    @Override
    public BaseMapper<T> getBaseDao() {
        return baseDao;
    }

    public void setBaseDao(BaseMapper<T> baseDao) {
        this.baseDao = baseDao;
    }


    public LcBaseMapper<T, Serializable> baseMapper;

    @Override
    public LcBaseMapper<T, Serializable> getBaseMapper() {
        return baseMapper;
    }

    public void setBaseMapper(LcBaseMapper<T, Serializable> baseMapper) {
        this.baseMapper = baseMapper;
    }


    @Override
    public int deleteByPrimaryKey(String id) {
        return baseMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByExample(Object o) {
        return baseMapper.deleteByExample(o);
    }

    @Override
    public int insertSelective(T record) {
        return baseMapper.insertSelective(record);
    }

    @Override
    public T selectByPrimaryKey(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return baseMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(T record) {
        return baseMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return baseMapper.updateByPrimaryKey(record);
    }


    @Override
    public List<T> selectByExample(Object example) {
        return baseMapper.selectByExample(example);
    }


    @Override
    public PageVo<T> getPageBean(Pageable pageable, Class<T> clazz) {
        return getPageBean(pageable, pageable.getPageSearch(), clazz);
    }


    @Override
    public PageVo<T> getPageBean(Pageable pageable, Map<String, Object> queryMap, Class<T> clazz) {

        System.out.println("startPage信息" + pageable.getPageIndex() + "--" + pageable.getPageSize());
        QueryWrapper<T> eWrapper = new QueryWrapper<>();
        try {
            eWrapper.setEntity(clazz.newInstance());
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        //拼接查询条件
        if (queryMap != null) {
            for (String key : queryMap.keySet()) {
                String value = queryMap.get(key).toString().trim();
                String[] conditionStrings = key.split("_");
                String condition = conditionStrings[1];
                //存在一种情况：查找的字段中带有下划线，所以截取第二个“_”之后的所有字符
                String fieldString = key.substring(key.indexOf("_", key.indexOf("_") + 1) + 1);
                // 条件信息
                switch (condition) {
                    case "LIKE":
                        if (StringUtils.isNotEmpty(value)){
                            eWrapper.like(fieldString, value);
                        }
                        break;
                    case "EQ":
                        if (StringUtils.isNotEmpty(value)){
                            eWrapper.like(fieldString, value);
                        }
                        break;
                    case "NOTEQ":
                        if (StringUtils.isNotEmpty(value)){
                            eWrapper.like(fieldString, value);
                        }
                        break;
                    case "IN":
                        if (StringUtils.isNotEmpty(value)){
                            eWrapper.like(fieldString, value);
                        }
                        break;
                    case "GE":
                        if (StringUtils.isNotEmpty(value)){
                            eWrapper.like(fieldString, value);
                        }
                        break;
                    case "LE":
                        if (StringUtils.isNotEmpty(value)){
                            eWrapper.like(fieldString, value);
                        }
                        break;
                    case "AS":
                            eWrapper.orderByAsc(value);
                        break;
                    case "DESC":
                            eWrapper.orderByDesc(value);
                        break;
                    default:
                        break;
                }
            }
        }

        Map<String, Boolean> orderParam = pageable.getPageOrderParam();
        for (String key : orderParam.keySet()) {
            Boolean ascFlag = orderParam.get(key);
            eWrapper.orderBy(true, ascFlag, key);
        }

        //pageHelper插件，分页封装
        PageHelper.startPage(pageable.getPageIndex(), pageable.getPageSize(), true);
        PageInfo<T> pageInfo = new PageInfo<>(super.getBaseMapper().selectList(eWrapper));
        return new PageVo<>(pageInfo);
    }


    public static void main(String[] args) {
        String str = "remark_filed_one_111";

        int index = str.indexOf("_");
        System.out.println(index);
        //根据第一个点的位置 获得第二个点的位置
        index = str.indexOf("_", index + 1);
        //根据第二个点的位置，截取 字符串。得到结果 result
        String result = str.substring(str.indexOf("_", str.indexOf("_") + 1) + 1);
        System.out.println(result + index);
    }


}
